/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import data.Data;
import java.io.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Riso-Desktop
 */
@WebServlet(name = "zapisBody", urlPatterns = {"/zapisBody"})
public class zapisBody extends HttpServlet {

    Data data;
    HttpSession session;
    String driver = "com.mysql.jdbc.Driver";
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    String id = "0";
    String uloha;
    
    
    @Override
    public void init() throws ServletException {
        pripojenie();
    }

    public void pripojenie() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "portalpredeti", "root", "");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    private synchronized String zapis(String body) {
        String s = null;
        try {
            stmt = con.createStatement();
            s = "Insert into vysledky (id_ziaka,typ_ulohy,body) values ( "+id+" , "+uloha+" , '"+body+"' )";
            stmt.executeUpdate("Insert into vysledky (id_ziaka,typ_ulohy,body) values ( "+id+" , "+uloha+" , '"+body+"' )");
            stmt.executeUpdate("Update user set udaje = null where id ="+id);
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s;
    }
    
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/x-java-serialized-object");
        try {
            InputStream in = request.getInputStream();
            ObjectInputStream inputFromApplet = new ObjectInputStream(in);
            uloha = (String) inputFromApplet.readObject();
            id = (String) inputFromApplet.readObject();
            String body = (String) inputFromApplet.readObject();
            inputFromApplet.close();
            String p = zapis(body);
            
            OutputStream outstr = response.getOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(outstr);
            oos.writeObject(p);
            oos.flush();
            oos.close();
        }catch(Exception e){
            e.printStackTrace();
        }finally {      
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
